package com.wxproject.demo.Service.impl;

import com.wxproject.demo.Entity.AO.PostAO;
import com.wxproject.demo.Entity.Model.PostDO;
import com.wxproject.demo.Entity.VO.Post.PagePostVO;
import com.wxproject.demo.Mapper.PostMapper;
import com.wxproject.demo.Service.PostService;
import com.wxproject.demo.Utils.DateUtils;
import com.wxproject.demo.Utils.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HCJ
 * @date 2021/4/24 16:41
 * @description
 */
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    PostMapper postMapper;
    @Override
    public Boolean createPost(PostAO postAO,String userId) {
        PostDO postDO = new PostDO();
        //复制传递的参数
        BeanUtils.copyProperties(postAO,postDO);
        //雪花生成随机ID
        postDO.setId(String.valueOf(SnowFlake.nextId()));
        //根据系统时间设置创建时间
        postDO.setCreateTime(DateUtils.getDate());
        //阅读量和收藏量初始为0
        postDO.setReadCount(0);
        postDO.setCollectionCount(0);
        //用户ID
        postDO.setUserId(userId);
        //插入数据
        Integer post = postMapper.createPost(postDO);
        if(post!=0){
            return true;
        }
        return false;
    }

    @Override
    public List<PagePostVO> getPostByPage(int page, int size) {
        int start;if(page==1||page==0){ start=0; }else{ start=(page-1)*size; }
        List<PagePostVO> postByPage = postMapper.getPostByPage(start, size);
        return postByPage;
    }



}
